View Javadoc
1   package org.apache.maven.surefire.junitcore.pc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.report.ConsoleLogger;
23  
24  import java.util.concurrent.CancellationException;
25  import java.util.concurrent.ConcurrentLinkedQueue;
26  import java.util.concurrent.ExecutionException;
27  import java.util.concurrent.ExecutorService;
28  import java.util.concurrent.Future;
29  
30  /**
31   * Parallel strategy for shared thread pool in private package.
32   *
33   * @author Tibor Digana (tibor17)
34   * @see AbstractThreadPoolStrategy
35   * @since 2.16
36   */
37  final class SharedThreadPoolStrategy
38      extends AbstractThreadPoolStrategy
39  {
40      SharedThreadPoolStrategy( ConsoleLogger logger, ExecutorService threadPool )
41      {
42          super( logger, threadPool, new ConcurrentLinkedQueue<Future<?>>() );
43      }
44  
45      @Override
46      public boolean hasSharedThreadPool()
47      {
48          return true;
49      }
50  
51      @Override
52      public boolean finished()
53          throws InterruptedException
54      {
55          boolean wasRunningAll = disable();
56          for ( Future<?> futureResult : getFutureResults() )
57          {
58              try
59              {
60                  futureResult.get();
61              }
62              catch ( InterruptedException e )
63              {
64                  // after called external ExecutorService#shutdownNow()
65                  wasRunningAll = false;
66              }
67              catch ( ExecutionException e )
68              {
69                  // JUnit core throws exception.
70                  if ( e.getCause() != null )
71                  {
72                      logQuietly( e.getCause() );
73                  }
74              }
75              catch ( CancellationException e )
76              {
77                  /**
78                   * Cancelled by {@link Future#cancel(boolean)} in {@link stop()} and {@link stopNow()}.
79                   */
80              }
81          }
82          return wasRunningAll;
83      }
84  
85      @Override
86      protected boolean stop()
87      {
88          return stop( false );
89      }
90  
91      @Override
92      protected boolean stopNow()
93      {
94          return stop( true );
95      }
96  
97      private boolean stop( boolean interrupt )
98      {
99          final boolean wasRunning = disable();
100         for ( Future<?> futureResult : getFutureResults() )
101         {
102             futureResult.cancel( interrupt );
103         }
104         return wasRunning;
105     }
106 }